/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.BrandFocusDao;
import com.jeesite.modules.storage.entity.BrandFocus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 品牌关注Service
 */
@Service
@Transactional(readOnly=true)
public class BrandFocusService extends CrudService<BrandFocusDao, BrandFocus> {

	/**
	 * 检查是否存在
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param brandId 品牌Id
	 * @return 是否存在
	 */
	public Boolean checkExist(String transMemberId, String memberId,String brandId){
		return dao.findExist(transMemberId,memberId,brandId);
	}

	/**
	 * 加入关注
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param brandId 品牌Id
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public String join(String transMemberId,String memberId, String brandId){
		BrandFocus brandFocus = new BrandFocus();
		brandFocus.setTransMemberId(transMemberId);
		brandFocus.setCreateBy(memberId);
		brandFocus.setBrandId(brandId);
		save(brandFocus);
		return "加入成功";
	}

	/**
	 * 取消关注
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param brandId 品牌Id
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public String cancel(String transMemberId,String memberId, String brandId){
		dao.phyDelete(new BrandFocus(dao.findId(transMemberId,memberId,brandId)));
		return "成功取消关注";
	}

	/**
	 * 查找关注品牌列表
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param size 每页数量
	 * @param page 页码
	 * @return 关注品牌列表
	 */
	public List<Map> getBrandList(String transMemberId,String memberId, String identity, Integer size, Integer page){
		List<Map> brandMaps = dao.findBrandList(transMemberId,memberId,identity.equals("Hot"),identity.equals("End"),size * (page - 1),size);
		for (Map brandMap : brandMaps){
			String brandId = String.valueOf(brandMap.get("brandId"));
			brandMap.put("isFocus",checkExist(transMemberId,memberId,brandId));
			brandMap.put("activities",dao.findActivitiesByBrandId(brandId));
		}
		return brandMaps;
	}
}
